package com.github.admin.edu.security.jwt.client.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.github.admin.edu.assembly.common.entity.JsonEntity;
import com.github.admin.edu.assembly.common.util.JsonUtils;
import com.github.admin.edu.assembly.string.util.StringUtils;
import com.github.admin.edu.oauth.server.api.entity.TokenEntity;
import com.github.admin.edu.security.jwt.client.domain.ServiceClientAuthDomain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018-7-20
 */
@Service
public class ClientHeaderTokenService {
    @Autowired
    private ServiceClientAuthDomain domain;

    @Value("${server.custom.server.server-id}")
    private String serverId;


    public String getToken(){
        String code= domain.getServiceToken(serverId);
        if(StringUtils.isNotBlank(code)) {
            JsonEntity<TokenEntity> jsonEntity = JsonUtils.toCollection(code,
                    new TypeReference<JsonEntity<TokenEntity>>() {
                    });
            if (null != jsonEntity && 200 == jsonEntity.getState()) {
                TokenEntity tokenEntity = jsonEntity.getData();
                if(null!=tokenEntity){
                    return tokenEntity.getToken();
                }
            }
        }
        return null;
    }
}
